/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.stress.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.springblade.common.cache.DictBizCache;
import org.springblade.common.constant.StressConstant;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.stress.entity.Channel;
import org.springblade.modules.stress.entity.Device;
import org.springblade.modules.stress.mapper.DeviceMapper;
import org.springblade.modules.stress.service.IChannelService;
import org.springblade.modules.stress.service.IDeviceService;
import org.springblade.modules.stress.vo.DeviceVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 起重设备管理信息表 服务实现类
 *
 * @author Yanzujian
 * @since 2021-01-10
 */
@Service
@AllArgsConstructor
public class DeviceServiceImpl extends BaseServiceImpl<DeviceMapper, Device> implements IDeviceService {

	private final IChannelService channelService;

	@Override
	public IPage<DeviceVO> selectDevicePage(IPage<DeviceVO> page, DeviceVO device) {
		return page.setRecords(baseMapper.selectDevicePage(page, device));
	}

	@Override
	@Transactional
	public boolean saveDevice(Device device) {
		boolean re = save(device);
		if (re){
			if (device.getId() > 0) {
				String deviceTypeName = DictBizCache.getValue("stress_device_type", device.getDeviceType());
				Channel channel = new Channel();
				channel.setChannelId(device.getId());
				channel.setParentId(0L);
				channel.setCode(device.getSelfCode());
				channel.setName(deviceTypeName + "(" +device.getDeviceModel()+")");
				channel.setSensorType(StressConstant.STRESS_SENSOR_TYPE_DEVICE);
				channel.setIsDeleted(BladeConstant.DB_NOT_DELETED);
				channelService.save(channel);
				return true;
			}
		}
		return false;
	}

	@Override
	@Transactional
	public boolean updateDevice(Device device) {
		boolean re = saveOrUpdate(device);
		if (re){
			String deviceTypeName = DictBizCache.getValue("stress_device_type", device.getDeviceType());
			Channel channel = channelService.getOne(Wrappers.<Channel>query().lambda().eq(Channel::getChannelId, device.getId()));
			channel.setCode(device.getSelfCode());
			channel.setName(deviceTypeName + "(" +device.getDeviceModel()+")");
			channelService.saveOrUpdate(channel);
			return true;
		}
		return false;
	}

	@Override
	@Transactional
	public boolean removeDevicesByIds(String ids) {
		//1、删除关联树
		channelService.remove(Wrappers.<Channel>query().lambda().eq(Channel::getSensorType, StressConstant.STRESS_SENSOR_TYPE_DEVICE).in(Channel::getChannelId, Func.toLongList(ids)));
		//2、删除本体
		return removeByIds(Func.toLongList(ids));
	}

}
